
import java.time.LocalDate;
import java.time.format.DateTimeParseException;
import java.util.*;
import java.io.*;
import java.text.*;


public class FileSystem {
	static boolean firstNum=true;
	public static void main(String[] args) {
		init();
		loadData();
		while (true){
			int week;	String[] choices={
			"1.按日期搜索",	"2.按周数搜索",	"3.按关键词搜索",	"0.退出",};
			switch (MyMenu.selectMenu(choices)){
				case 2: {
					if ((week= GoldfishReader.readInt("请输入周数"))
							<= totalArrangements.size()&& week > 0)
							System.out.println(totalArrangements.get(week - 1).toString(100));
					else	System.out.println("超出教学周范围");
				}break;
				case 1:while (true){
						try {
							searchDate(GoldfishReader.readString("请输入日期(如11.25):"));
							break;
						}catch (Exception e){System.out.println("无效日期格式 请重试");}
					}break;
				case 3:
					searchKeyword(GoldfishReader.readString("请输入关键字"));break;
				case 0:
					System.out.println("欢迎再次使用");
					System.exit(0);
			}
		}
	}
	static LinkedList<WeeklyEvents> totalArrangements;
	static String path="src\\read.txt";
	static Scanner scanner,fileScanner;
	static void handle(Exception exception){
		System.out.println(exception.getMessage());
		scanner.nextLine();
		System.exit(0);
	}
	static void init(){
		totalArrangements=new LinkedList<>();
		scanner=new Scanner(System.in);
		while (true)
		{
			try {
				fileScanner = new Scanner(new File(path));
				break;
			} catch (IOException ioException) {
				System.out.println("无效路径，请手动输入路径或输入0退出");
				path=scanner.next();
				if(path.equals("0")){
					System.out.println("欢迎下次使用！");
					System.exit(0);
				}
			}


			catch (Exception e){
				System.out.println(e.getMessage());
				break;
			}
		}
	}
	static void loadData(){
		while (true){
			try {
				WeeklyEvents weeklyEvents=newWeek();
				if(weeklyEvents==null)	break;
				totalArrangements.add(weeklyEvents);
			}
			catch (NoSuchElementException noSuchElementException){break;}
		}
	}
	static WeeklyEvents newWeek()
			throws NoSuchElementException {
		String buf;
		String[] split;
		boolean dateRead=false;
		int weekNum=1,eventNum,temp;
		String start;
		String end;
		String eventContent;
		WeeklyEvents weeklyEvents=null;
		while (true){
			do{
				buf = fileScanner.nextLine();
			}while (buf.isEmpty()||buf.isBlank());
			try{
				weekNum=Integer.parseInt(buf);
				if(firstNum)firstNum=false;
				else return weeklyEvents;
			}
			catch (NumberFormatException numberFormatException){
				if(dateRead){
					try {
						split = buf.split("\\.");
						eventNum=Integer.parseInt(split[0]);
						eventContent=split[split.length-1].trim();
						weeklyEvents.add(eventNum,eventContent);
					}
					catch (Exception e){FileSystem.handle(e);}
				}
				else
				{
					split = buf.split("-");
					start = split[0];
					end = split[split.length - 1];
					weeklyEvents=new WeeklyEvents(weekNum,start,end);
					dateRead=true;
				}
			}
		}
	}
	static void searchDate(String dateBuf)
	throws DateTimeParseException,ArrayIndexOutOfBoundsException{
		boolean break_=false;
		LocalDate searchDate=LocalDate.now();
		searchDate = WeeklyEvents.localDate(dateBuf);
		for(WeeklyEvents weeklyEvents:totalArrangements)
			if(weeklyEvents.showEvents(searchDate)){break_=true;break;}
		if(break_) System.out.println("\n");
		else System.out.println("未查询到匹配结果");
	}
	static void searchKeyword(String searchBuf){
		boolean break_=false;
		for (WeeklyEvents weeklyEvents : totalArrangements) {
			break_=false;
			System.out.println(weeklyEvents);
			if (weeklyEvents.showEvent(searchBuf)) {
				break_ = true;
			}
			else System.out.println("本周没有相应安排");
		}
		if (!break_) System.out.println(searchBuf + ":没有匹配结果");
	}
}
class WeeklyEvents{
	LinkedList<String> events;
	int weekNum;
	SimpleDateFormat simpleDateFormat;
	LocalDate startDate,endDate;
	WeeklyEvents(){}
	public WeeklyEvents(int weekNum,
						String startDate, String endDate) {
		this.simpleDateFormat=new SimpleDateFormat("yyyy.MM.dd");
		this.events = new LinkedList<>();
		this.weekNum=weekNum;
		try{
			this.startDate = localDate(startDate);
			this.endDate = localDate(endDate);
		}
		catch (Exception e){
			FileSystem.handle(e);
		}
	}
	public String toString(int index){
		StringBuilder stringBuilder=new StringBuilder(toString()).append('\n');
		try{
			stringBuilder.append(events.get(index));
			return new String(stringBuilder);
		}catch (IndexOutOfBoundsException indexOutOfBoundsException){
			if(index<0){
				return new String(stringBuilder);
			}
			for(String event:events)
				stringBuilder.append(event).append("\n");
			return new String(stringBuilder);
		}
	}
	@Override
	public String toString() {
		return this.startDate.toString().replace("-",".")
				+"~"
				+this.endDate.toString().replace("-",".");
	}
	void add(String event){
		events.add(event);
	}
	void add(int eventNum,String event){
		add(eventNum+"\t"+event);
	}
	private static Date myParseDate(String s)throws ParseException{
		SimpleDateFormat simpleDateFormat=new SimpleDateFormat(
				"yyyy.MM.dd");
		String[] split=s.split("\\.");
		if(split.length<=1)return null;
		if(Integer.parseInt(split[0])<=7)
				return simpleDateFormat.parse("2023"+s);
		else	return simpleDateFormat.parse("2022"+s);
	}
	public static LocalDate localDate(String s)
		throws IndexOutOfBoundsException,
			DateTimeParseException{

		String[] split=s.split("\\.");
		if(split.length<=1)return null;
		else;

		StringBuilder stringBuilder=new StringBuilder("20");
		if(Integer.parseInt(split[0])<=7)
				stringBuilder.append("23");
		else	stringBuilder.append("22");

		stringBuilder.append("-");

		if(Integer.parseInt(split[0])<10)
			stringBuilder.append("0");
		stringBuilder.append(split[0]);

		stringBuilder.append("-");

		if(Integer.parseInt(split[1])<10)
			stringBuilder.append("0");
		stringBuilder.append(split[1]);
		return LocalDate.parse(new String(stringBuilder));
	}
	public boolean showEvents(LocalDate date){
		if(date.isAfter(startDate)&&date.isBefore(endDate)){
			for (String event:events) System.out.println(event);
			return true;
		}return false;
	}
	public boolean showEvent(String comp){
		boolean res=false;
		for(String event:events){
			boolean shown=false;
			for(int i=0;i<=event.length()-comp.length();i++)
				if(event.regionMatches(i,comp,0,comp.length())) {
					res=true;
					if (!shown) {System.out.println(event);shown = true;}
				}
		}
		return res;
	}
}